﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Parse;
using ParseControls;

namespace ParseOnlineAdmin
{
    /// <summary>
    /// Interaction logic for ParseRelatedProperties.xaml
    /// </summary>
    public partial class ParseRelatedProperties : UserControl
    {
        public ParseRelatedProperties()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this.LoadItems();
        }

        private void LoadItems()
        {
            this.stpControls.Children.Clear();
            Guid classGUID = Guid.Empty;
            ParseDataContext ctx = ((HostWindow)Application.Current.MainWindow).DataContext as ParseDataContext;
            if (ctx != null)
            {
                if (ctx.SelectedClass != null)
                {
                    classGUID = ((ParseClass)ctx.SelectedClass).GUID;
                }
            }

            if (classGUID == Guid.Empty)
                return;

            foreach (ParseProperty _property in ParseOne.Package.Properties
                .Where(item => item.ParseClassGUID.ToString() == classGUID.ToString()))
            {
                ParseRelatedPropertyItemView view =
                    new ParseRelatedPropertyItemView(_property.Name);

                this.stpControls.Children.Add(view);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Guid classGUID = Guid.Empty;

            // Get Context 
            ParseDataContext ctx = ((HostWindow)Application.Current.MainWindow).DataContext as ParseDataContext;
            if (ctx != null)
            {
                if (ctx.SelectedClass != null)
                {
                    classGUID = ((ParseClass)ctx.SelectedClass).GUID;
                }
            }

            // Check 
            if (ctx == null)
                return;

            ParseObject po = ctx.SelectedItem as ParseObject;
            if (po == null)
                return;

            if (classGUID == Guid.Empty)
                return;

            if (this.txtValue.Text == string.Empty)
                return;

            string _property = this.txtProperty.Text;

            if (this.txtProperty.Text == string.Empty)
                return;

            ParseProperty existsProp = ParseOne.Package.Properties.Where(item => item.Name == _property).FirstOrDefault();
            if (existsProp != null)
            {
                // Save Current Object 
                po[_property] = this.txtValue.Text;
                ParseOne.InformationSending(new ParseCustomEventArgs("Save:Current:Parse:Object"));

                // Reload Property Items with Binding 
                this.LoadItems();
                return;
            }

            // Property 
            ParseProperty prop = new ParseProperty();
            prop.GUID = Guid.NewGuid();
            prop.Name = this.txtProperty.Text;
            prop.ParseClassGUID = classGUID;

            // Package 
            ParseOne.Package.Properties.Add(prop);
            ParseOne.Package.Save();
            ParseOne.ReloadPackage();

            // Save Current Object 
            po[_property] = this.txtValue.Text;
            ParseOne.InformationSending(new ParseCustomEventArgs("Save:Current:Parse:Object"));

            // Reload Property Items with Binding 
            this.LoadItems();
        }
    }
}
